/**
 * ADC1 Generated Driver File
 *
 * @file adc1.c
 * 
 * @ingroup adc1 
 * 
 * @brief API Implementations for ADC1 module driver.
 * 
 * @version ADC1 Driver Version 1.0.0
*/

/*
? [2023] Microchip Technology Inc. and its subsidiaries.

    Subject to your compliance with these terms, you may use Microchip 
    software and any derivatives exclusively with Microchip products. 
    You are responsible for complying with 3rd party license terms  
    applicable to your use of 3rd party software (including open source  
    software) that may accompany Microchip software. SOFTWARE IS ?AS IS.? 
    NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS 
    SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT,  
    MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 
    WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, 
    INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY 
    KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 
    MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 
    FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP?S 
    TOTAL LIABILITY ON ALL CLAIMS RELATED TO THE SOFTWARE WILL NOT 
    EXCEED AMOUNT OF FEES, IF ANY, YOU PAID DIRECTLY TO MICROCHIP FOR 
    THIS SOFTWARE.
*/


#include "../adc1.h"

adc_irq_cb_t ADC1_window_cb = NULL;
adc_irq_cb_t ADC1_resrdy_cb = NULL;

int8_t ADC1_Initialize(void)
{
     
    // DUTYCYC DUTY25; 
    ADC1.CALIB = 0x1;

    // SAMPNUM 1 ADC sample; 
    ADC1.CTRLB = 0x0;

    // PRESC CLK_PER divided by 16; REFSEL VDDREF; SAMPCAP enabled; 
    ADC1.CTRLC = 0x53;

    // ASDV ASVOFF; INITDLY DLY16; SAMPDLY 0x0; 
    ADC1.CTRLD = 0x20;

    // WINCM No Window Comparison; 
    ADC1.CTRLE = 0x0;

    // DBGRUN disabled; 
    ADC1.DBGCTRL = 0x0;

    // STARTEI disabled; 
    ADC1.EVCTRL = 0x0;

    // RESRDY disabled; WCMP disabled; 
    ADC1.INTCTRL = 0x0;

    // MUXPOS ADC input pin 0; 
    ADC1.MUXPOS = 0x0;

    // SAMPLEN 3; 
    ADC1.SAMPCTRL = 0x3;

    // Window comparator high threshold 
    ADC1.WINHT = 0x3FF;

    // Window comparator low threshold 
    ADC1.WINLT = 0x0;

    // ENABLE enabled; FREERUN disabled; RESSEL 10-bit mode; RUNSTBY disabled; 
    ADC1.CTRLA = 0x1;


    return 0;
}

void ADC1_Enable(void)
{
    ADC1.CTRLA |= ADC_ENABLE_bm;
}

void ADC1_Disable(void)
{
    ADC1.CTRLA &= ~ADC_ENABLE_bm;
}

void ADC1_EnableAutoTrigger(void)
{
    ADC1.EVCTRL |= ADC_STARTEI_bm;
}

void ADC1_DisableAutoTrigger(void)
{
    ADC1.EVCTRL &= ~ADC_STARTEI_bm;
}

void ADC1_SetWindowHigh(adc_result_t high)
{
    ADC1.WINHT = high;
}

void ADC1_SetWindowLow(adc_result_t low)
{
    ADC1.WINLT = low;
}

void ADC1_SetWindowMode(ADC1_window_mode_t mode)
{
    ADC1.CTRLE = mode;
}

void ADC1_SetWindowChannel(adc_0_channel_t channel)
{
    ADC1.MUXPOS = channel;
}

void ADC1_StartConversion(adc_0_channel_t channel)
{
    ADC1.MUXPOS  = channel;
    ADC1.COMMAND = ADC_STCONV_bm;
}

bool ADC1_IsConversionDone(void)
{
    return (ADC1.INTFLAGS & ADC_RESRDY_bm);
}

adc_result_t ADC1_GetConversionResult(void)
{
    return (ADC1.RES);
}

bool ADC1_GetWindowResult(void)
{
    bool temp     = (ADC1.INTFLAGS & ADC_WCMP_bm);
    ADC1.INTFLAGS = ADC_WCMP_bm; // Clear intflag if set
    return temp;
}

adc_result_t ADC1_GetConversion(adc_0_channel_t channel)
{
    adc_result_t res;

    ADC1_StartConversion(channel);
    while (!ADC1_IsConversionDone());
    res           = ADC1_GetConversionResult();
    ADC1.INTFLAGS = ADC_RESRDY_bm;
    return res;
}

uint8_t ADC1_GetResolution(void)
{
    return (ADC1.CTRLA & ADC_RESSEL_bm) ? 10 : 12;
}

void ADC1_RegisterWindowCallback(adc_irq_cb_t f)
{
    ADC1_window_cb = f;
}

void ADC1_RegisterResrdyCallback(adc_irq_cb_t f)
{
    ADC1_resrdy_cb = f;
}

ISR(ADC1_WCOMP_vect)
{        
    // Clear the interrupt flag
    ADC1.INTFLAGS = ADC_WCMP_bm;

    if (ADC1_window_cb != NULL)
    {
        ADC1_window_cb();
    }
}

ISR(ADC1_RESRDY_vect)
{
    // Clear the interrupt flag
    ADC1.INTFLAGS = ADC_RESRDY_bm;

    if (ADC1_resrdy_cb != NULL)
    {
        ADC1_resrdy_cb();
    }
}